package com.gis.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.swing.JTable;

public class MySQLConnection implements DBConnection {
	public Connection connect = null;
//	public static void test() {
//	    String url = "jdbc:mysql://localhost:3306/";
//	    String dbName = "test";
//	    String driver = "com.mysql.jdbc.Driver";
//	    String userName = "root"; 
//	    String password = "josh";
//	    try {
//		  Class.forName(driver).newInstance();
//		  connect = DriverManager.getConnection(url+dbName,userName,password);
//		  Statement state = connect.createStatement();
//		  ResultSet results = state.executeQuery("show databases");
//		//  state.execute("ALTER TABLE `test`.`gis_table_1` ADD COLUMN `col4` VARCHAR(45) NULL  AFTER `col2` ;");
//		  //ResultSet results = state.executeQuery("select * from test.gis_table_1");
//		 while (results.next()) {
//		 System.out.println(results.getString("Database"));
//		 }
////		  System.out.println("Connected to the database");
////		  conn.close();
////		  System.out.println("Disconnected from database");
//	    } catch (Exception e) {
//	      e.printStackTrace();
//	    }
//	}
//	
	public static void main(String[] args) {
		
	}

	@Override
	public boolean connect() {
		Connection conn = null;
	    String url = "jdbc:mysql://localhost:3306/";
	    String dbName = "test";
	    String driver = "com.mysql.jdbc.Driver";
	    String userName = "root"; 
	    String password = "josh";
	    try {
		  Class.forName(driver).newInstance();
		  connect = DriverManager.getConnection(url+dbName,userName,password);
	    } catch (SQLException ex) {
	    	
	    } catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    return connect != null;
	}

	@Override
	public boolean disconnect() {
		try {
			connect.close();
			return connect.isClosed();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean execute(String cmd) {
		Statement state;
		try {
			state = connect.createStatement();
			return state.execute(cmd);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public ResultSet query(String cmd) {
		Statement state;
		try {
			state = connect.createStatement();
			return state.executeQuery(cmd);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public DBType getDBType() {
		return DBType.MYSQL;
	}

}
